package com.ccit.integral.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ccit.common.exception.GymException;
import com.ccit.common.result.Result;
import com.ccit.common.utils.PageUtil;
import com.ccit.common.utils.RedisUtils;
import com.ccit.common.utils.UploadUtil;
import com.ccit.integral.service.IntegralGoodsService;
import com.ccit.integral.service.IntegralRecordService;
import com.ccit.integral.service.impl.IntegralServiceImpl;
import com.ccit.model.Integral;
import com.ccit.model.IntegralGoods;
import com.ccit.model.IntegralRecord;
import com.ccit.model.vo.integral.IntegralRecordVo;
import com.ccit.model.vo.integral.IntrgralVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "兑换管理")
@RestController
@RequestMapping("/integralRecord")
public class IntegralRecordController {
    @Autowired
    private IntegralServiceImpl integralService;
    @Autowired
    private IntegralGoodsService integralGoodsService;
    @Autowired
    private IntegralRecordService integralRecordService;

    @Autowired
    private RedisUtils redisUtils;


    @ApiOperation(value = "获取所有积分兑换")
    @GetMapping("/getAllIntegralRecord")
    public Result getAllIntegralRecord() {
        String key = "AllIntegralRecord";
        boolean hasKey = redisUtils.hasKey(key);
        List<IntegralRecord> recordList;
        if (hasKey) {
            recordList = (List<IntegralRecord>)redisUtils.get(key);
        } else {
            List<IntegralRecord> list = integralRecordService.list();
            recordList = list;
            redisUtils.set(key, recordList);
        }
        return Result.ok(recordList);
    }

    @ApiOperation(value = "分页获取手机号兑换记录")
    @GetMapping("/getPageIntegralRecord/{current}/{size}")
    public Result getIntegralRecordByPhone(@RequestParam(required = false)  String phone,
                              @PathVariable int current,
                              @PathVariable int size) {
        Page<IntegralRecordVo> page;
        String key = "IntegralRecordPage:" + current + ":" + size;
        if (phone!=null){
            key = "IntegralRecordPage:"+ phone + ":" + current + ":" + size;
        }
        boolean hasKey = redisUtils.hasKey(key);
        if (hasKey){
            System.out.println("==============redis=====================");
            page = (Page<IntegralRecordVo>)redisUtils.get(key);
        }else {
            QueryWrapper<IntegralRecord> wrapper = new QueryWrapper<IntegralRecord>();
            if (phone != null){
                wrapper.lambda()
                        .like(IntegralRecord::getPhone, phone);
            }
            List<IntegralRecord> list = integralRecordService.list(wrapper);
            List<IntegralRecordVo> integralRecordVoList = new ArrayList<>();
            list.forEach(integralRecord -> {
                IntegralGoods integralGoods = integralGoodsService.getById(integralRecord.getIntegralGoodsId());
                IntegralRecordVo integralRecordVo = new IntegralRecordVo();
                integralRecordVo.setId(integralRecord.getId());
                integralRecordVo.setIntegralGoods(integralGoods);
                integralRecordVo.setPhone(integralRecord.getPhone());
                integralRecordVo.setTime(integralRecord.getTime());
                integralRecordVoList.add(integralRecordVo);
            });
            page = PageUtil.getPages(current, size, integralRecordVoList);
            redisUtils.set(key,page);
        }
        return Result.ok(page);
    }


    @ApiOperation(value = "删除兑换记录")
    @DeleteMapping("/deleteIntegralRecord/{id}")
    public Result removeIntegral(@PathVariable("id") Integer id/*,@RequestPart String code*/) {
//        if (redisUtils.get("code").equals(code)){
            boolean byId = integralRecordService.removeById(id);
            if (byId) {
                redisUtils.deleteRedisKeys("AllIntegralRecord");
                redisUtils.deleteRedisKeys("IntegralRecordPage");
                return Result.ok("删除成功");
            } else throw new GymException(202, "无此积分用户");
//        }else throw new GymException(210, "验证码错误");
    }



}
